
package com.yimixian.app.viewholder;

import android.animation.Animator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Point;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.umeng.analytics.MobclickAgent;
import com.yimixian.app.EventBusDomain.CartItemRemoveMessage;
import com.yimixian.app.R;
import com.yimixian.app.SystemFramework;
import com.yimixian.app.cart.CartDetailActivity;
import com.yimixian.app.cart.CouDanActivity;
import com.yimixian.app.cart.CouDanFragment;
import com.yimixian.app.cart.HuanGouActivity;
import com.yimixian.app.common.Constants;
import com.yimixian.app.common.UMengConstants;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.fragment.CartFragment;
import com.yimixian.app.model.CartUpdateDescription;
import com.yimixian.app.model.GoodsItem;
import com.yimixian.app.ui.AutoMoreTextLinearlayout;
import com.yimixian.app.ui.PlusMinusIcon;
import com.yimixian.app.util.StringUtils;
import com.yimixian.app.util.Strings;
import com.yimixian.app.util.UiUtils;
import com.ymx.sdk.animator.animatorUtils;
import com.ymx.sdk.widget.recyclerview.viewholder.AbsViewHolder;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import de.greenrobot.event.EventBus;

/**
 * Created by songzy on 2015/11/27.
 */
public class CartItemViewHolder extends AbsViewHolder {

    @InjectView(R.id.name_text)
    TextView mNameText;
    @InjectView(R.id.price_text)
    TextView mPriceText;
    @InjectView(R.id.plus_minus_icon_container)
    LinearLayout mPlucMinusIconContainer;
    @InjectView(R.id.iv_good_img)
    ImageView mGoodImage;
    @InjectView(R.id.unit_text)
    TextView mUnitText;
    @InjectView(R.id.text_tips)
    TextView mTextTips;
    @InjectView(R.id.fl_masking)
    FrameLayout mFlMasking;// 当物品数量为0时候的蒙版
    // @InjectView(R.id.divider_view) View mDividerView;
    @InjectView(R.id.item_remove)
    ImageView mItemRemove;
    @InjectView(R.id.freebie_title_text)
    TextView mFreebieTitleText;
    @InjectView(R.id.tv_huan_gou)
    TextView tv_huan_gou;
    @InjectView(R.id.rl_huan_gou)
    RelativeLayout rl_huan_gou;
//    @InjectView(R.id.service_text)
//    TextView serviceText;
    @InjectView(R.id.lin_bottom)
    LinearLayout linBottom;
    @InjectView(R.id.promotion_image)
    ImageView mPromotionImage;
    @InjectView(R.id.to_buy_more)
    TextView toBuyMore;
    @InjectView(R.id.auto_more_text_lin)
    AutoMoreTextLinearlayout mAutoMoreTextLinearlayout;

    protected GoodsItem mGoodsItem;
    private PlusMinusIcon mPlusMinusIcon;
    private List<GoodsItem> mGoodsItems;
    private Context mContext;
    private android.os.Handler mHandler = new android.os.Handler();

    public CartItemViewHolder(View itemView, Context context) {
        super(itemView, context);
        itemView.setBackgroundColor(Color.WHITE);
        mContext = context;
        ButterKnife.inject(this, itemView);
    }

    @Override
    public void bindItemData(Object data) {
        if (data instanceof GoodsItem) {
            final GoodsItem goodsItem = (GoodsItem) data;
            mGoodsItem = goodsItem;
            if (mGoodsItem == null) {
                return;
            }

            if (mPlusMinusIcon == null) {
                mPlusMinusIcon = new PlusMinusIcon(SystemFramework.getInstance().getGlobalContext(),
                        goodsItem, Constants.STATE_CARTFRAGMENT_BUTTON_BROADCAST);
                mPlucMinusIconContainer.addView(mPlusMinusIcon);
            } else {
                mPlusMinusIcon.updateGoodsItem(goodsItem);
                if (goodsItem != null) {
                    mPlusMinusIcon.setQuantity(Integer.parseInt(goodsItem.count));
                } else {
                    mPlusMinusIcon.setQuantity(0);
                }
            }
            if (mPlusMinusIcon.getQuantity() == 0) {
                showFLMasking();
            } else {
                hideFLMasking();
            }

            mItemRemove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CartItemRemoveMessage message = new CartItemRemoveMessage();
                    message.goodsId = goodsItem.id;
                    int position = 0;
                    if(mGoodsItems != null && mGoodsItems.size() > 0) {
                        for(int i=0;i<mGoodsItems.size();i++) {
                            if(mGoodsItems.get(i) != null && mGoodsItems.get(i).id == goodsItem.id) {
                                position = i;
                                break;
                            }
                        }
                    }
                    message.position = position;
                    EventBus.getDefault().post(message);
                }
            });
            if(mContext != null && mContext instanceof CartDetailActivity) {
                if (((CartDetailActivity) mContext).mIsEditState) {
                    mItemRemove.setAlpha(0.0f);
                    mItemRemove.setVisibility(View.VISIBLE);
                    if (CartFragment.isNeedAnimator) {
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mItemRemove.setAlpha(1.0f);
                                Animator animator1 = animatorUtils.translateX(mPlucMinusIconContainer, 0, -1 * mItemRemove.getWidth(), 200, new LinearInterpolator());
                                Animator animator = animatorUtils.translateX(mItemRemove, mItemRemove.getWidth(), 0, 200, new LinearInterpolator());
                                animator.start();
                                animator1.start();
                            }
                        }, 10);
                    } else {
                        mItemRemove.setAlpha(1.0f);
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                Animator animator1 = animatorUtils.translateX(mPlucMinusIconContainer, 0, -1 * mItemRemove.getWidth(), 0, new LinearInterpolator());
                                animator1.start();
                                mItemRemove.setVisibility(View.VISIBLE);
                            }
                        }, 10);
                    }
                } else {
                    if(CartFragment.isNeedAnimator) {
                        Animator animator = animatorUtils.translateX(mPlucMinusIconContainer, -1 * mItemRemove.getWidth(), 0, 200, new LinearInterpolator());
                        Animator animator1 = animatorUtils.translateX(mItemRemove, 0, mItemRemove.getWidth(), 200, new LinearInterpolator());
                        animator1.addListener(new Animator.AnimatorListener() {
                            @Override
                            public void onAnimationStart(Animator animation) {
                            }
                            @Override
                            public void onAnimationEnd(Animator animation) {
                                mItemRemove.setVisibility(View.GONE);
                            }
                            @Override
                            public void onAnimationCancel(Animator animation) {
                            }
                            @Override
                            public void onAnimationRepeat(Animator animation) {
                            }
                        });
                        animator1.start();
                        animator.start();
                    } else {
                        mItemRemove.setVisibility(View.GONE);
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                Animator animator1 = animatorUtils.translateX(mPlucMinusIconContainer, -1 * mItemRemove.getWidth(), 0, 0, new LinearInterpolator());
                                animator1.start();
                            }
                        }, 10);
                    }
                }
            } else {
                mItemRemove.setVisibility(View.GONE);
            }
            mNameText.setText(goodsItem.name);
            mUnitText.setText(goodsItem.unitDesc);
            mPriceText.setText("¥" + goodsItem.unitPrice);
            if(mContext != null && mContext instanceof Activity) {
                Display display = ((Activity) mContext).getWindowManager().getDefaultDisplay();
                Point size = new Point();
                display.getSize(size);
                int screenWidth = size.x;
                Picasso.with(mContext)
                    .load(DataManager.getInstance().getImageUrl(goodsItem.img, screenWidth / 2))
                    .placeholder(R.drawable.placeholder).into(mGoodImage);
            }
            if (Strings.isNullOrEmpty(mGoodsItem.promotionImg)) {
                mPromotionImage.setVisibility(View.GONE);
            } else if (mContext != null){
                mPromotionImage.setVisibility(View.VISIBLE);
                int size = mContext.getResources().getDimensionPixelSize(R.dimen.jiao_biao_size);
                Picasso.with(mContext).load(DataManager.getInstance().getImageUrl(
                        mGoodsItem.promotionImg, size)).into(mPromotionImage);
            }
            if(goodsItem.isNeedBuy && !StringUtils.isNullOrEmpty(goodsItem.but_name))
                toBuyMore.setText(goodsItem.but_name);
            toBuyMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                            UMengConstants.NEW_CART_ADDONBTN);
                    if (mContext != null) {
//                        mContext.startActivity(YMXMainActivity.buildIntentTab(mContext,
//                                YMXMainActivity.TAB_KIND));
//                        ((CartDetailActivity)mContext).finish();
//                        Intent intent = new Intent(mContext, CouDanActivity.class);
//                        intent.putExtra("pici", goodsItem.pici);
                        ((Activity)mContext).startActivityForResult(CouDanActivity.buildIntent(mContext).putExtra("pici", goodsItem.pici)
                                .putExtra("group_id", goodsItem.getGroupId() + ""), 0);
                        ((Activity)mContext).overridePendingTransition(R.anim.activity_slide_right_in, R.anim.activity_slide_right_out);
//                        ((CartDetailActivity)mContext).finish();
                    }

                }
            });

            if(!goodsItem.isActivated) {
                showFLMasking();
            } else {
                hideFLMasking();
            }
            if(TextUtils.isEmpty(goodsItem.tips)) {
                hideTextTips();
            } else {
                setTextTips(goodsItem.tips);
                showTextTips();
            }

            //2.3.0
            rl_huan_gou.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                            UMengConstants.NEW_CART_REDEMPTION);
                    Intent intent = new Intent(mContext, HuanGouActivity.class)
                            .putExtra("group_id", goodsItem.getGroupId() + "");
                    ((Activity) mContext).startActivityForResult(intent, 0);
                }
            });

        }
    }

    public void showFLMasking() {
        if (mFlMasking != null) {
            mFlMasking.setVisibility(View.VISIBLE);
        }
        //新需求 换购商品可以点击减号删除
//        if (mPlusMinusIcon != null) {
//            mPlusMinusIcon.setButtonCanClick(false);
//        }
    }

    public void hideFLMasking() {
        if (mFlMasking != null) {
            mFlMasking.setVisibility(View.GONE);
        }
        if (mPlusMinusIcon != null) {
            mPlusMinusIcon.setButtonCanClick(true);
        }
    }

    public void setTextTips(String tips) {
        if (mTextTips != null) {
            mTextTips.setText(tips);
        }
    }

    public void hideTextTips() {
        if (mTextTips != null) {
            mTextTips.setVisibility(View.GONE);
        }
    }

    public void showTextTips() {
        if (mTextTips != null) {
            mTextTips.setVisibility(View.VISIBLE);
        }
    }
    public void hideTitleView() {
        if(mFreebieTitleText != null) {
            mFreebieTitleText.setVisibility(View.GONE);
        }
    }

    public void showTitleView() {
        if(mFreebieTitleText != null) {
            mFreebieTitleText.setVisibility(View.VISIBLE);
        }
    }

    public void setTitle(String title) {
        if(mFreebieTitleText != null) {
            mFreebieTitleText.setText(title);
        }
    }

    public void showHuanGouView(String text){
        if(rl_huan_gou != null && tv_huan_gou != null) {
            tv_huan_gou.setText(text);
            rl_huan_gou.setVisibility(View.VISIBLE);
        }
    }

    public void hideHuanGouView(){
        if(rl_huan_gou != null) {
            rl_huan_gou.setVisibility(View.GONE);
        }
    }

    public void isNeedShowBottom(boolean isShow, List<CartUpdateDescription> simpleDesc, boolean isNeedBuy){
        if (simpleDesc != null && isShow){
            linBottom.setVisibility(View.VISIBLE);
//            serviceText.setText(text);
            mAutoMoreTextLinearlayout.setInitData(simpleDesc, "#666666", 12);
            toBuyMore.setVisibility(isNeedBuy ? View.VISIBLE : View.GONE);
        }else{
            linBottom.setVisibility(View.GONE);
            toBuyMore.setVisibility(View.GONE);
        }
//        toBuyMore.setVisibility(View.VISIBLE);
    }

    public void setDatas(List<GoodsItem> goodsItems) {
        mGoodsItems = goodsItems;
    }

}
